#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "../DEMURA/Demura.h"
#include "usbd_cdc.h"
#include "fsmc.h"

/********************************************************************************************
V3 Hefei Gen Demura port
********************************************************************************************

********************************************************************************************/


/********************************************************************************************
//合肥  上位机按照先低位后高位依次顺序，下位机也按此顺序写入补偿资料，增加 buffer offset 功能 for and 前面需要添加地址情况
********************************************************************************************/

void SSD2828_W_RAM_demura_123rgb(float pclk,u8 SigMode ,u8* buffer ,u16 length,u8  multiple ,u8 start_offset)
{
  	u32 i;
	 u32 id_off; //index_offset
    u32 Data_Num = 0;
    Data_Num = length*multiple*3;

	#ifdef SSD_Command_16
	for(i=0;i<(Data_Num/6);i++)
	{	
		id_off=i*6+start_offset;
		Write_DATA((buffer[id_off+1]<<8)+buffer[id_off]);//210, 543
		delay_6ns(1);
		Write_DATA((buffer[id_off+5]<<8)+buffer[id_off+2]);
		delay_6ns(1);
		Write_DATA((buffer[id_off+3]<<4)+buffer[id_off+4]);								
		delay_6ns(1);	
	}
	#else	
	for(i=0;i<Data_Num/3;i++)
	{
		Write_DATA(buffer[i*3+2]);
		Write_DATA(buffer[i*3+1]);
		Write_DATA(buffer[i*3]);          
	}
	#endif 
}




void Demura2RAM_HFGen(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	USB_Rx_Demura_End_Flag=0;	
	if(USB_Rx_Demura==0)//第一笔数据
	{												
		Demura_first_package_flag=0;		
		//DemuraTransOnce=180*3;
		//DemuraTransOnce=OLED.H_pixel*3;					
		DemuraTransOnce=((DemuraSize_H8)<<8)+DemuraSize_L8;		
		USB_Rx_Demura=1; //进入Demura数据写入状态			
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);			
	}
		
		//	SSD2828_W_Reg(OLED.SigMode,CS_Master,0xB7,HS_B7_Data);	
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //写入单笔数据,单次写入DemuraTransOnce个字节
	{			
		switch(DemuraIctype)//  00-只下2C as pic;01 只有第一笔添加addr;02 =每笔都有不同数据 分别DemuraAddr_1st，DemuraAddr_2nd
		{			
			case 0x00:  //不用添加下包地址，如图片一样只有数据例如NOVA NT37701 
			{					
				if(Demura_first_package_flag!=0)
				{   //2832 似乎默认送3C	  
					if(DemuraRecivedNum!=USB_Rx_Demura_Total_Num){	
						SPI_CS_Select(channel,0);
						SSD2828_W_RAM_buffer_8bits_part_NT37701(OLED.pixel_clk,OLED.SigMode,buffer,VCP_Receive_True_num,1,0);
						SPI_CS_Select(channel,1);	
					}
					else //(DemuraRecivedNum==USB_Rx_Demura_Total_Num)
					{	
						uint16_t len=0;
						len= USB_Rx_Demura_Total_Num%DemuraTransOnce;
						//len 必须为2的倍数
						SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (len)); 
						SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((len)>>16)); 
						SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_2nd);	
						SPI_CS_Select(channel,0);
						SSD2828_W_RAM_buffer_8bits_part_NT37701(OLED.pixel_clk,OLED.SigMode,buffer,len,1,0);  //len 必须为2的倍数
						SPI_CS_Select(channel,1);
					}
						
				}
				else //(Demura_first_package_flag==0)
				{
					Demura_first_package_flag=1;
					SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
					SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 
					//SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(DemuraSize_H8<<8)+DemuraSize_L8);
					SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
					SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_1st);	

					SPI_CS_Select(channel,0);
					SSD2828_W_RAM_buffer_8bits_part_NT37701(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
					SPI_CS_Select(channel,1);								
				}
				
				break;
			}			
			case 0x01:  //只是第一笔加地址 需要增加register 地址,第二笔不加
					{			
						if(Demura_first_package_flag==0)//第一笔
						{						
							Demura_first_package_flag=1;	
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce+1)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce+1)>>16)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
							SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + DemuraAddr_1st));  //第一笔数据地址eg:2C或4C 

							SPI_CS_Select(channel,0);							
							SSD2828_W_RAM_demura_123rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,1); //  offset=1
							SPI_CS_Select(channel,1);
						}		
						else//第二笔
						{
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,VCP_Receive_True_num);  
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 		
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						//	SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + DemuraAddr_2nd));  //第二笔数据无需添加地址 
							SPI_CS_Select(channel,0);
							SSD2828_W_RAM_demura_123rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
							SPI_CS_Select(channel,1);
						}								
						break;
					}
			case 0x02:  //第一个包和第二个包的地址不同,需要增加register 地址,chipone 通用
					{				
							if(Demura_first_package_flag==0)//第一笔
							{						
								Demura_first_package_flag=1;	
									
								SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
								SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce+1)); 
								SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce+1)>>16)); 
								SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
								SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
								SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + DemuraAddr_1st));  //第一笔数据地址eg:2C或4C 

								SPI_CS_Select(channel,0);							
								SSD2828_W_RAM_demura_123rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,1); //  offset=1
								SPI_CS_Select(channel,1);
							}		
							else//第二笔
							{
								SSD2828_W_Reg(OLED.SigMode,channel,0xBC,VCP_Receive_True_num+1);  
								SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num+1)>>16)); 		
								SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
								SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + DemuraAddr_2nd));  //第一笔数据地址eg:2C或4C 
								SPI_CS_Select(channel,0);
								SSD2828_W_RAM_demura_123rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,1);
								SPI_CS_Select(channel,1);
							}								
								break;
						}
			case 0x10:  //bit[1]=1 --数据流 顺序 rgb,  bit[3]~bit[0] CMD 类型 
					{					
						if(Demura_first_package_flag==0)
						{
							Demura_first_package_flag=1;
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						//	SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
							SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_1st);	 
						}
						else
						{
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 	
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
							//SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);								
							SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_2nd);	  
						}											
						SPI_CS_Select(channel,0);
						SSD2828_W_RAM_demura_buffer_part_rgb(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
						SPI_CS_Select(channel,1);	
						break;
					}
			case 0x30:  //不用添加下包地址，如图片一样只有数据例如NOVA NT37701 
					{					
						if(Demura_first_package_flag==0)
						{
							Demura_first_package_flag=1;
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (DemuraTransOnce)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((DemuraTransOnce)>>16)); 
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						//	SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);	
							SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_1st);	

						SPI_CS_Select(channel,0);
						SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
						SPI_CS_Select(channel,1);								
						}
						else
						{
							SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (VCP_Receive_True_num)); 
							SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((VCP_Receive_True_num)>>16)); 	
							SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						//	SSD2828_W_Cmd(OLED.SigMode,channel,0x3C);								
							SSD2828_W_Cmd(OLED.SigMode,channel,DemuraAddr_2nd);	  
						SPI_CS_Select(channel,0);
						SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,VCP_Receive_True_num,1,0);
						SPI_CS_Select(channel,1);	
						}											
						break;
				}	
				
			default:break;	
	
			}						
			VCP_Receive_True_num=0;
			
			if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
			{
				Demura_START_FLAG=0;  			//清除Demura状态
				DemuraRecivedNum=0; 			//累计接收统计 归零
				USB_Rx_Demura=0;	 			 //清空 Demura downloading状态
				Demura_first_package_flag=0; 		//Novatek_NT37701 4c/5c
				//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xC0,0x0001);
				buffer[4]=0x5a;buffer[5]=0;buffer[6]=0xA5;
				STM2PC(pdev,CDC_IN_EP,buffer,8);      //返回上位机10个byte数据				
			}		
	
	}	
}



//********************************************************************************************
